class RouteInstancesController < ApplicationController
  # GET /route_instances
  # GET /route_instances.json
  def index
    @route_instances = RouteInstance.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @route_instances }
    end
  end

  # GET /route_instances/1
  # GET /route_instances/1.json
  def show
    @route_instance = RouteInstance.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @route_instance }
    end
  end

  # GET /route_instances/new
  # GET /route_instances/new.json
  def new
    @route_instance = RouteInstance.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @route_instance }
    end
  end

  # GET /route_instances/1/edit
  def edit
    @route_instance = RouteInstance.find(params[:id])
  end

  # POST /route_instances
  # POST /route_instances.json
  def create
    @route_instance = RouteInstance.new(params[:route_instance])

	@route_instance.stop_instances.each_with_index { |s,i|
	  s.stop_number = i + 1
	}
	
    respond_to do |format|
      if @route_instance.save
        flash[:notice] = "Courier assignment successfully updated for #{@route_instance.courier.real_name} on #{@route_instance.day.strftime("%a %b %e")}."
        format.html { redirect_to(edit_route_path @route_instance.route) }
        format.json  { render :json => @route_instance, :status => :created, :location => @route_instance }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @route_instance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /route_instances/1
  # PUT /route_instances/1.json
  def update
    params[:route_instance][:existing_stop_instance_attributes] ||= {}
  
    @route_instance = RouteInstance.find(params[:id])
	
    respond_to do |format|
      if @route_instance.update_attributes(params[:route_instance])
	  
	    # won't see new stops if done before respond_to
	  	@route_instance.stop_instances.each_with_index { |s,i|
	      s.stop_number = i + 1 # cross fingers
		  s.save
	    }
	  
        flash[:notice] = "Courier assignment successfully updated for #{@route_instance.courier.real_name} on #{@route_instance.day.strftime("%a %b %e")}."
        format.html { redirect_to(edit_route_path @route_instance.route) }
        format.json  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @route_instance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /route_instances/1
  # DELETE /route_instances/1.json
  def destroy
    @route_instance = RouteInstance.find(params[:id])
    @route_instance.destroy

    respond_to do |format|
	  flash[:notice] = "Courier assignment successfully updated for #{@route_instance.courier.real_name} on #{@route_instance.day.strftime("%a %b %e")}."
	  format.html { redirect_to(edit_route_path @route_instance.route) }
      format.json  { head :ok }
    end
  end
end
